package com.leng.util;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class MailUtil {
	private String hostName;
	private String from;// 发送人邮箱
	private String authorization;// 授权码
	private String title;// 发送人显示的名字
	private int port;

	public MailUtil(String hostName, String from, String authorization, String title, int port) {
		super();
		this.hostName = hostName;
		this.from = from;
		this.authorization = authorization;
		this.title = title;
		this.port = port;
	}

	/**
	 * @throws Exception
	 * @throws EmailException
	 * 			@Title: sendMail @Description: 发送邮件 @param: @param subject
	 *             主题 @param: @param email 收件人 @return: void @throws
	 */
	public void sendMail(String subject, String mail, String msg) throws Exception {
		HtmlEmail email = new HtmlEmail();
		// 这里是SMTP发送服务器的名字：163的如下："smtp.163.com"
		// email.setHostName("smtp.163.com");
		email.setHostName(hostName);
		email.setSmtpPort(port);
		email.setSSLOnConnect(true);
		// 字符编码集的设置
		email.setCharset("utf-8");
		// 收件人的邮箱
		email.addTo(mail);
		// 发送人的邮箱
		// email.setFrom("lengmianshi@163.com", "AutoSign贴吧签到");
		email.setFrom(from, title);
		// 如果需要认证信息的话，设置认证：用户名-密码。分别为发件人在邮件服务器上的注册名称和密码
		// email.setAuthentication("lengmianshi@163.com", "leng123456");
		email.setAuthentication(from, authorization);
		// 要发送的邮件主题
		email.setSubject(subject);
		// 要发送的信息，由于使用了HtmlEmail，可以在邮件内容中使用HTML标签
		email.setMsg(msg);
		// 发送
		email.send();
	}

	public static void main(String[] args) {
		try {
			String content = "<html><head>" + "<style>" + "div{" + " color:red;" + " font-size:30px;" + "}" + "</style>"
					+ "</head>" + "<body>" + "" + "   <div>测试测试测试</div> " + "  <div>呵呵 </div></body></html>";
			MailUtil util = new MailUtil("smtp.qq.com", "lengmianshi@qq.com", "djvmybyvxrfjiaif", "AutoSign贴吧签到", 465);
			util.sendMail("测试", "lengmianshi@163.com", content);

			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
